package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.TOutageFeedbackMapper;
import com.ruoyi.system.domain.TOutageFeedback;
import com.ruoyi.system.service.ITOutageFeedbackService;

/**
 * 停电反馈数据Service业务层处理
 * 
 * @author zhangkai
 * @date 2021-12-10
 */
@Service
public class TOutageFeedbackServiceImpl implements ITOutageFeedbackService 
{
	private static final Logger log = LoggerFactory.getLogger(TOutageFeedbackServiceImpl.class);
    @Autowired
    private TOutageFeedbackMapper tOutageFeedbackMapper;

    /**
     * 查询停电反馈数据
     * 
     * @param id 停电反馈数据主键
     * @return 停电反馈数据
     */
    @Override
    public TOutageFeedback selectTOutageFeedbackById(String id)
    {
        return tOutageFeedbackMapper.selectTOutageFeedbackById(id);
    }

    /**
     * 查询停电反馈数据列表
     * 
     * @param tOutageFeedback 停电反馈数据
     * @return 停电反馈数据
     */
    @Override
    public List<TOutageFeedback> selectTOutageFeedbackList(TOutageFeedback tOutageFeedback)
    {
        return tOutageFeedbackMapper.selectTOutageFeedbackList(tOutageFeedback);
    }

    /**
     * 新增停电反馈数据
     * 
     * @param tOutageFeedback 停电反馈数据
     * @return 结果
     */
    @Override
    public int insertTOutageFeedback(TOutageFeedback tOutageFeedback)
    {
        tOutageFeedback.setCreateTime(DateUtils.getNowDate());
        tOutageFeedback.setUuid(UUID.randomUUID().toString().replace("-", ""));
        return tOutageFeedbackMapper.insertTOutageFeedback(tOutageFeedback);
    }

    /**
     * 修改停电反馈数据
     * 
     * @param tOutageFeedback 停电反馈数据
     * @return 结果
     */
    @Override
    public int updateTOutageFeedback(TOutageFeedback tOutageFeedback)
    {
        tOutageFeedback.setUpdateTime(DateUtils.getNowDate());
        return tOutageFeedbackMapper.updateTOutageFeedback(tOutageFeedback);
    }
    
    public int updateTOutageFeedbackForFengongshi(TOutageFeedback tOutageFeedback)
    {
    	tOutageFeedback.setReasonTime(DateUtils.getNowDate());

        return tOutageFeedbackMapper.updateTOutageFeedbackForFengongshi(tOutageFeedback);
    }

    /**
     * 批量删除停电反馈数据
     * 
     * @param ids 需要删除的停电反馈数据主键
     * @return 结果
     */
    @Override
    public int deleteTOutageFeedbackByIds(String[] ids)
    {
        return tOutageFeedbackMapper.deleteTOutageFeedbackByIds(ids);
    }

    /**
     * 删除停电反馈数据信息
     * 
     * @param id 停电反馈数据主键
     * @return 结果
     */
    @Override
    public int deleteTOutageFeedbackById(Long id)
    {
        return tOutageFeedbackMapper.deleteTOutageFeedbackById(id);
    }
    
    /**
     * 导入全站数据
     * 
     * @param totalList 全站数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importTotal(List<TOutageFeedback> totalList,String operName,String dateRange, String depName, String userName)
    {
    	operName = SecurityUtils.getLoginUser().getUsername();
        Long userid = SecurityUtils.getLoginUser().getUserId();
        
    	if((depName.contains("辽宁省") || "admin".equals(userName)) && dateRange.equals("null")) {
    		throw new ServiceException("版本不能为空！");
    	}
    	String newDateRange = dateRange;
        if (StringUtils.isNull(totalList) || totalList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TOutageFeedback site : totalList) {
            try {

                site.setUpdateBy(operName);
                if(!(depName.contains("辽宁省") || "admin".equals(userName))){
//                    site.setVersion(newDateRange);
                    site.setReasonTime(new Date());
                    site.setReasonBy(operName);
                    this.updateTOutageFeedbackForFengongshi(site);
                }else if(StringUtils.isNull(site.getUuid())){
                    site.setVersion(newDateRange);
                    site.setUpdateBy(operName);
                    site.setUpdateTime(new Date());
                    this.insertTOutageFeedback(site);
                } else{
                    this.updateTOutageFeedback(site);
                }
                successNum++;
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、站址运维ID: " + site.getStationOperationId() + " 导入失败：";
                failureMsg.append(msg + "系统异常");
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
